<template>
	<div>

		<div class="app-container">
			<TopInfo />
			<Search :lookup="data.lookup" @investigate="inves" />

			<el-button type="primary" v-prmis="'config_scene_add'" size="small" @click="add(1)">
				<template #default>
					<span class="text-sm">{{ $t('manage.scene.add') }}</span>
				</template>
			</el-button>

		</div>

		<div class="app-container" v-loading="data.mtloading">
			<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
				:cell-style="{ 'text-align': 'center' }" class="w-full">
				<el-table-column prop="id" label="ID" />
				<el-table-column prop="name" :label="$t('manage.scene.name')" />

				<el-table-column prop="status" :label="$t('manage.scene.state')">
					<template #default="scope">
						<el-tag :type="scope.row.status == 1 ? 'success' : scope.row.status == 2 ? 'danger' : 'info'">
							{{ $t(getPlevel.statustype(scope.row.status, 1)) }}
						</el-tag>
					</template>
				</el-table-column>

				<el-table-column prop="desc" :label="$t('manage.scene.desc')">
					<template #default="scope">
						{{ scope.row.desc || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="creator_nickname" :label="$t('source.creater')">
					<template #default="scope">
						{{ scope.row.creator_nickname || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="create_time" :label="$t('date.createTime')">
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.create_time, 3) }}
					</template>
				</el-table-column>

				<el-table-column prop="update_time" :label="$t('date.modifyTime')">
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.update_time, 3) }}
					</template>
				</el-table-column>



				<el-table-column :label="$t('public.operate')">
					<template #default="scope">
						<el-button v-prmis="'config_scene_update'" type="primary" size="small"
							@click="add(2, scope.row)"><el-icon>
								<EditPen />
							</el-icon></el-button>
						<el-button v-prmis="'config_scene_del'" type="danger" size="small"
							@click="onDel(scope.row)"><el-icon>
								<Delete />
							</el-icon></el-button>
					</template>
				</el-table-column>

			</el-table>
			<PageNum :total="data.total" :page="data.lang" @exposeData="current" @fraction="fraction" />
		</div>

		<el-dialog v-model="data.dialogVisible" :title="data.title" center width="30%">
			<el-form v-if="data.dialogVisible" :model="data.addscenes" :rules="data.rules" :label-width="language=='en'?'120px':'100px'"
				ref="ruleFormRef">

				<el-form-item :label="$t('manage.scene.name') + ':'" prop="name">
					<el-input v-model="data.addscenes.name" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>

				<el-form-item :label="$t('manage.scene.desc') + ':'" prop="desc">
					<el-input v-model="data.addscenes.desc" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>

				<el-form-item :label="$t('manage.scene.state') + ':'" prop="type">
					<el-select v-model="data.addscenes.status" :placeholder="$t('message.pleaseSelect')">
						<el-option v-for="item in getPlevel.statustype('', 2)" :key="item.value" :label="$t(item.label)"
							:value="item.value" />
					</el-select>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="submit(ruleFormRef)">{{ $t('public.save') }}</el-button>
					<el-button @click="data.dialogVisible = false">{{ $t('public.cancel') }}</el-button>
				</el-form-item>

			</el-form>
		</el-dialog>

	</div>

</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import getPlevel from '@/utils/level'
import { getscene, addscene, updatescene, delscene } from '@/api/manage';
import { onMounted } from 'vue'
let language = ref()
const { locale } = useI18n()
onMounted(() => {
	language.value = locale.value
})
let ruleFormRef = ref<any>()
let data = reactive({
	mtloading: true, // 加载
	dialogVisible: false, // 弹窗
	lookup: [ // 筛选栏
		{ title: $t('manage.scene.name'), type: 'input', value: '', key: 'name' },
	],
	list: [], // 列表信息
	rules: { // 表单验证
		name: { required: true, message:  $t('message.pleaseEnter'), trigger: 'blur' },
	},
	title: '', // 弹窗标题
	addFlng: 1,
	addscenes: { // 添加/修改
		name: '',
		desc: '',
		status: 1,
		id: 1,
	},
	scene: { // 筛选数据
		name: '',
		page: 1,
		pageNum: 15
	},
	total: 0, // 总条数

})
// 查询列表
let getscenelist = () => {
	data.mtloading = true
	getscene(data.scene).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			data.list = res.data.data
			data.total = res.data.total
		}
	})
}

// 查询回调
let inves = (e) => {
	for (let key in e) {
		data.scene[key] = e[key]
	}
	data.scene.page = 1
	data.scene.pageNum = 15
	getscenelist()
}

// 页数回调
let current = (e) => {
	data.scene.page = e
	getscenelist()
}


// 条数回调
let fraction = (e) => {
	data.scene.pageNum = e
	getscenelist()
}

// 删除
let onDel = (row: any) => {
	ElMessageBox.confirm($t('message.delData'))
		.then(res => {
			delscene({ id: row.id }).then(res => {
				if (res.state == 200) {
					ElMessage({
						message: $t('message.delSuccess'),
						type: 'success'
					})
					data.dialogVisible = false
					getscenelist()
					return
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				})
			})
		}).catch(err => {
			console.log('取消')
		})
}
// 添加按钮
let add = (flng: number, row: any) => {

	if (flng == 1) {
		data.addscenes = {
			name: '',
			desc: '',
			status: 1,
		}
		data.title = $t('manage.scene.add')
		data.addFlng = flng
	}
	if (flng == 2) {
		data.addscenes = {
			id: row.id,
			name: row.name,
			desc: row.desc,
			status: 1,
		}
		data.title = $t('manage.scene.modify')
		data.addFlng = flng
	}
	data.dialogVisible = true
}

// 保存按钮
let submit = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid, fields) => {
		if (valid) {
			if (data.addFlng == 1) {
				addscene(data.addscenes).then(res => {
					data.dialogVisible = false
					if (res.state == 200) {
						ElMessage({
							message: $t('message.modifySuccess'),
							type: 'success'
						})
						getscenelist()
						return
					}
					ElMessage({
						message: $t('message.networkFailed'),
						type: 'error'
					})
				})
			}
			if (data.addFlng == 2) {
				updatescene(data.addscenes).then(res => {
					data.dialogVisible = false
					if (res.state == 200) {
						ElMessage({
							message: $t('message.modifySuccess'),
							type: 'success'
						})
						getscenelist()
						return
					}
					ElMessage({
						message: $t('message.networkFailed'),
						type: 'error'
					})
				})
			}

		}
	})
}

getscenelist()

</script>
